package cn.xhz.gdal.action;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.xhz.gdal.config.CommonConfig;
import cn.xhz.gdal.model.GDAL;
import cn.xhz.gdal.service.GDALDeleteService;
import cn.xhz.gdal.util.Assert;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Feature;
import org.gdal.ogr.Layer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 官方提供了删除的方法
 * 可以直接删除整个图层
 * 也可以删除图层的某个数据
 * layer.DeleteFeature(feature.GetFID());
 * layer.DeleteField()
 */
public class GDALDelete extends CommonConfig {
    private final Log log= LogFactory.get();

    /**
     * 给数据源操作
     */
    private GDAL gdal;

    public static GDALDelete gdal(GDAL gdal){
        GDALDelete gdalDelete = new GDALDelete();
        gdalDelete.gdal=gdal;
        return gdalDelete;
    }

    public DataSourceServiceImpl dataSourceService(){
        return new DataSourceServiceImpl();
    }
    public RowServiceImpl rowService(){
        return new RowServiceImpl();
    }
    public LayerServiceImpl layerService(){
        return new LayerServiceImpl();
    }

    public class DataSourceServiceImpl implements GDALDeleteService{
        @Override
        public void delete() {
            checkRequired();
            getDriver().DeleteDataSource(getPath());
        }
    }
    public class RowServiceImpl implements GDALDeleteService{
        /**
         * 一个或者多个
         */
        private List<Feature> features;
        private List<Long> fids;
        public RowServiceImpl feature(Feature feature){
            this.features.add(feature);
            return this;
        }
        public RowServiceImpl fid(Long fid){
            this.fids.add(fid);
            return this;
        }
        @Override
        public void delete() {
            Assert.isNotNull(gdal.getLayer(),"请传递图层对象");
            HashSet<Long> fidSet = new HashSet<>();
            fidSet.addAll(fids);
            fidSet.addAll(features.stream().map(Feature::GetFID).collect(Collectors.toList()));
            fidSet.forEach(k->gdal.getLayer().DeleteFeature(k));
        }
    }
    public class LayerServiceImpl implements GDALDeleteService {
        private List<Integer> layerIndexList;

        public LayerServiceImpl layerIndex(Integer layerIndex){
            this.layerIndexList.add(layerIndex);
            return this;
        }
        @Override
        public void delete() {
            //需要告诉当前图层
            DataSource dataSource = gdal.getDataSource();
            Assert.isNotNull(dataSource,"请传递数据源对象");
            if (layerIndexList.size()==0) {
                Layer layer = gdal.getLayer();
                Assert.isNotNull(layer,"推荐传递需要删除图层下标，备选传递图层对象未传递");
                //暂时不做直接报错
                throw new NullPointerException("请传递删除数据下标");
            }
            for (Integer index : layerIndexList) {
                log.info("当前数据源{}的{}图层即将删除!",dataSource.getName(),dataSource.GetLayer(index).GetName());
                try {
                    dataSource.DeleteLayer(index);
                    log.info("删除成功!");
                } catch (Exception e) {
                    log.error("删除失败!");
                }
            }
        }
    }

    public GDAL getGdal() {
        return gdal;
    }

    public void setGdal(GDAL gdal) {
        this.gdal = gdal;
    }


}
